using System;
using System.Collections.Generic;

namespace shfa.snomtastic.data.business
{
    public class SubnetComparer : IComparer<SubnetInfo>
    {
        private string _sortExpression;
        public string SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

		private string _sortDirection;
        public string SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }

        public SubnetComparer(string sortExpression)
        {
            int iSplitPos = sortExpression.IndexOf(" ");
            SortExpression = (iSplitPos > 0) ? sortExpression.Substring(0, iSplitPos) : sortExpression;
            SortDirection = (iSplitPos > 0) ? sortExpression.Substring(iSplitPos + 1).ToUpper() : "ASC";
        }

        public SubnetComparer(string sortExpression, string sortDirection)
        {
            this.SortExpression = sortExpression;
            this.SortDirection = sortDirection;
        }

        int IComparer<SubnetInfo>.Compare(SubnetInfo x, SubnetInfo y)
        {
            SubnetInfo oX = (SortDirection == "ASC") ? x : y;
            SubnetInfo oY = (SortDirection == "ASC") ? y : x;

            switch (SortExpression)
            {				
				case "SubnetId":
					return oX.SubnetId.CompareTo(oY.SubnetId);				
				case "ConfigurationId":
					return oX.ConfigurationId.CompareTo(oY.ConfigurationId);				
				case "IP":
					return oX.IP.CompareTo(oY.IP);				
				case "Mask":
					return oX.Mask.CompareTo(oY.Mask);				
				case "StartIP":
					return oX.StartIP.CompareTo(oY.StartIP);				
				case "EndIP":
					return oX.EndIP.CompareTo(oY.EndIP);				
				case "StartInt":
					return oX.StartInt.CompareTo(oY.StartInt);				
				case "EndInt":
					return oX.EndInt.CompareTo(oY.EndInt);
				default:
                    return 0;
            }
        }
    }
}
